//----------------------------------Include-------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mcu.h"
#include "glbvar.h"
#include "dtu.h"
//------------------------------------------------------------------------------
//global var defined in this file
#define  CLOUD_GLOBALS
#include "cloud.h"
//------------------------------------------------------------------------------

//---------------------------------Global Var-----------------------------------
//------------------------------------------------------------------------------

//-----------------------------------Macro--------------------------------------
//for tx
#define TXBUF_LEN                   (256)
//------------------------------------------------------------------------------
//for fsm
#define RUN_START                   0
#define RUN_READY                   1
#define RUN_BUFF                    2
//------------------------------------------------------------------------------

//---------------------------------Static Var-----------------------------------
//for tx
static char             ntxbuff[TXBUF_LEN];
//------------------------------------------------------------------------------
//for fsm
static uint32_t         nu32_rs        = RUN_START;
static uint64_t         nu64_time_tick = 0;
//------------------------------------------------------------------------------
static int              n_rdm_tick;
//------------------------------------------------------------------------------

//--------------------------------Static Func-----------------------------------
static void _up_data(void)
{
  uint32_t   pwr_s;
  pwr_s = mcu_outpwr_status();
  sprintf(ntxbuff, "%s=%d", ga_imei, pwr_s);
  dtu_wr_buf(ntxbuff, strlen(ntxbuff));
}
//------------------------------------------------------------------------------

//--------------------------------Public Func-----------------------------------
void cloud_parse(char* pbuf, uint32_t sz)
{
  char imei[DTU_IMEI_LEN+1];
  if(sz < DTU_IMEI_LEN+2) return;
  
  for(uint32_t i=0; i<DTU_IMEI_LEN; i++){ imei[i] = pbuf[i]; }
  imei[DTU_IMEI_LEN] = 0;
  if(strcmp(imei, ga_imei) == 0){
    if     (pbuf[DTU_IMEI_LEN+1] == '1') mcu_out_pwr(1);
    else if(pbuf[DTU_IMEI_LEN+1] == '0') mcu_out_pwr(0);
  }
}
//------------------------------------------------------------------------------
void cloud_cyc(void)
{
  switch(nu32_rs){
  case RUN_START:
    _up_data();
    nu64_time_tick = igu64_tick_ms;
    nu32_rs = RUN_BUFF;
    break;
  case RUN_READY:
    if(igu64_tick_ms > nu64_time_tick + n_rdm_tick + 30*1000){
      srand(igu64_tick_ms);
      n_rdm_tick = rand()%30000;
      
     _up_data();
        
      nu64_time_tick = igu64_tick_ms;
      nu32_rs = RUN_BUFF;
    }
    break;
  case RUN_BUFF:
    if(igu64_tick_ms > nu64_time_tick + 1000){
      dtu_push();
      nu64_time_tick = igu64_tick_ms;
      nu32_rs = RUN_READY;
    }
    break;
  default:break;
  }
}
//-------------------------------------EOF--------------------------------------







